use rocket::data::{FromData, Outcome, ToByteUnit};
use rocket::fs::TempFile;
use rocket::{Data, data, Request};
use rocket::form::Form;
use rocket::serde::json::Json;
use rocket::serde::Deserialize;
use rocket::tokio::fs::File;
use serde::Deserializer;
use crate::工具包::File分片::{分片, 分片构造器};

#[derive(FromForm)]
pub struct PieceForm<'a> {
	id:String,
	piece_number:String,// 0:完成分片,可以合成
	file_data:TempFile<'a>
}



#[post("/file",data = "<file>")]
pub async fn post_file_cs(mut file: Form<PieceForm<'_>>) -> std::io::Result<String> {
	println!("{}",&file.id);
	println!("{}",&file.piece_number);
	let piece_number = file.piece_number.clone();
	let piece = 分片构造器::new(file.id.clone());
	if file.piece_number.eq("0") {
		if let Some((file,i))= piece.make_file() {
			
			println!("文件合成完成!");
			println!("总分片数:{}",i);
			println!("文件:{:#?}",file);
			return Ok(format!("Ok:合成成功,总片数:[{}]",i));
		}
		return Ok(format!("Err:合成失败"))
	}
	
	file.file_data.persist_to(piece
		.path().join(&piece_number)).await?;//它居然会直接建立文件...让我的函数显得很没用耶
	
	Ok(piece_number)
}

